<!--  -->
<template>
  <div class="app-home">
    <top-header></top-header>
    <h1>数据统计</h1>
    <div class="app-home-container">
      <div class="home-container-box">
        <div class="container-box-left">
          <span class="title">总用户量:</span>
          <span class="num">{{ userTotalCount }}</span>
        </div>
        <div class="container-box-right">
          <span class="title1">注册用户:</span>
          <span class="num1">{{ userCount }}</span>
        </div>
      </div>
      <div class="home-container-box">
        <div class="container-box-left">
          <span class="title">总订单量:</span>
          <span class="num">{{ orderTotalCount }}</span>
        </div>
        <div class="container-box-right">
          <span class="title1">新增订单:</span>
          <span class="num1">{{ orderCount }}</span>
        </div>
      </div>
      <div class="home-container-box">
        <div class="container-box-left">
          <span class="title">管理员:</span>
          <span class="num">{{ adminTotalCount }}</span>
        </div>
        <div class="container-box-right">
          <span class="title1">新增管理员:</span>
          <span class="num1">{{ adminCount }}</span>
        </div>
      </div>
    </div>
    <tendency :serven-day="servenDay" :serven-day-data="servenDayData"></tendency>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import Header from "@/components/header/Header";
import {
  getUserCount,
  getAllUserCount,
  getOrderCount,
  getAllOrderCount,
  getAdminCount,
  getAllAdminCount,
} from "../../api/api";
// DateUtils.getFormatDate => 2020-10-12
import { DateUtils } from "../../Utils/utils";
import Tendency from "../charts/tendency"
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {
    "top-header": Header,
    "tendency":Tendency
  },
  data() {
    //这里存放数据
    return {
      userTotalCount: 0,
      userCount: 0,
      orderTotalCount: 0,
      orderCount: 0,
      adminTotalCount: 0,
      adminCount: 0,
      servenDay:[],// 最近七天的字符串 [...,"2020-10-19","2020-10-20"]
      servenDayData:[[],[],[]] // 七天对应的数据
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
      getSevenDaysData:function(){
          console.log("获取最近七天的数据");
          // 最近七天的字符串 [...,"2020-10-19","2020-10-20"]
          let servenDay = [];
          for(let i=6;i>=0;i--){
              let dateString = DateUtils.getFormatDate(new Date(new Date().getTime()-24*60*60*1000*i));
              servenDay.push(dateString);
          }
          let apiArr = [[],[],[]];// 新增注册用户 新增订单 新增管理员
          servenDay.map(v=>{ // v => 2020-10-12
              apiArr[0].push(getUserCount(v));
              apiArr[1].push(getOrderCount(v));
              apiArr[2].push(getAdminCount(v));
          })
          let servenDayData = [[],[],[]];
          // 总价 21请求 
          Promise.all([...apiArr[0],...apiArr[1],...apiArr[2]]).then((res)=>{
             res.map((v,i)=>{
                 // 将请求分别放在不同的数组中
                 servenDayData[Math.floor(i/7)].push(v.count);
             })
          })
          console.log(servenDayData);
          console.log(servenDay);
          this.servenDay = servenDay;
          this.servenDayData = servenDayData;
      }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    let today = DateUtils.getFormatDate(new Date());
    console.log(today);
    Promise.all([
      getAllUserCount(),
      getUserCount(today),
      getAllOrderCount(),
      getOrderCount(today),
      getAllAdminCount(),
      getAdminCount(today),
    ]).then((res) => {
      console.log(res);
      this.userTotalCount = res[0].count;
      this.userCount = res[1].count;
      this.orderTotalCount = res[2].count;
      this.orderCount = res[3].count;
      this.adminTotalCount = res[4].count;
      this.adminCount = res[5].count;
    });
    this.getSevenDaysData();
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='less' scoped>
@import url("../../style/common.less");
.app-home {
  background-color: white;
  height: 100%;
  h1 {
    text-align: center;
    font-weight: 300;
    height: 50px;
    line-height: 50px;
  }
  .app-home-container {
    display: flex;
    .home-container-box {
      height: 80px;
      border-top: 1px solid #ddd;
      border-right: 1px solid #ddd;
      border-bottom: 1px solid #ddd;
      flex: 1;
      display: flex;
      .container-box-left {
        .fc();
        flex: 1;
        .num {
          font-size: 30px;
          font-weight: 200;
          color: #ff9800;
        }

        .title {
        }
      }

      .container-box-right {
        .fc();
        flex: 1;
        .title1 {
        }

        .num1 {
          font-size: 30px;
          font-weight: 200;
          color: #20a0ff;
        }
      }
    }
  }
}
</style>